import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart' hide Action;
import 'package:flutter/scheduler.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:hayll_app_village_master/hayll/network/yll_network.dart';
import 'package:hayll_app_village_master/hayll/village_manager/home/company_component/action.dart';
import 'package:hayll_app_village_master/hayll/village_manager/home/main_page/bean/company.dart';
import 'package:hayll_app_village_master/hayll/village_manager/home/main_page/bean/village.dart';
import 'package:hayll_app_village_master/hayll/village_manager/network/village_action.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'action.dart';
import 'state.dart';

Effect<MainState> buildEffect() {
  return combineEffects(<Object, Effect<MainState>>{
    Lifecycle.initState: _init,
    MainAction.switchCompany: _switchCompany,
    MainAction.switchVillage: _switchVillage,
  });
}

void _init(Action action, Context<MainState> ctx) {
  YllNetwork.requestPost(VillageAction.getCompany, params: {},
      success: (json) async {
    var comanyResult = CompanyListResult.fromJson(json);
    ctx.state.commanyList = comanyResult.data;
    ctx.state.selectedCompany = null;
    if (comanyResult.data != null && comanyResult.data.isNotEmpty) {
      //获取上次选中的小区
      var sp = await SharedPreferences.getInstance();
      var lastCompanyId = sp.getInt('lastCompany');
      if (lastCompanyId != null) {
        for (CompanyResult item in comanyResult.data) {
          if (item.companyId == lastCompanyId) {
            ctx.state.selectedCompany = item;
            break;
          }
        }
      }
      //如果没有找到该公司
      if (ctx.state.selectedCompany == null) {
        var selectCompany = comanyResult.data.first;
        ctx.state.selectedCompany = selectCompany;
        //缓存上次选中的小区
        sp.setInt('lastCompany', selectCompany.companyId);
      }
    }
    if (ctx.state.selectedCompany != null) {
      //获取小区列表
      getVillageListByCompany(ctx, ctx.state.selectedCompany.companyId);
    }
    ctx.dispatch(MainActionCreator.refreshUI());
  }, failed: (errorCode, errorMsg) {
    Fluttertoast.showToast(msg: '$errorCode,$errorMsg');
  });
}

void _switchCompany(Action action, Context<MainState> ctx) async {
  if (ctx.state.commanyList != null) {
    for (CompanyResult item in ctx.state.commanyList) {
      if (item.companyId == action.payload) {
        ctx.state.selectedCompany = item;
        ctx.dispatch(MainActionCreator.refreshUI());
        //缓存上次选中的小区
        var sp = await SharedPreferences.getInstance();
        sp.setInt('lastCompany', item.companyId);
        //获取小区列表
        getVillageListByCompany(ctx, item.companyId);
        break;
      }
    }
  }
}

void _switchVillage(Action action, Context<MainState> ctx) async {
  if (ctx.state.commanyList != null) {
    for (VillageSimpleResult item in ctx.state.villageList) {
      if (item.villageId == action.payload) {
        ctx.state.selectedVillage = item;
        //刷新下层数据
        ctx.dispatch(MainActionCreator.refreshUI());
        //
        //缓存上次选中的小区
        var sp = await SharedPreferences.getInstance();
        sp.setInt('lastVillage', item.villageId);
        ctx.dispatch(CompanyActionCreator.initUI());
        break;
      }
    }
  }
}

void getVillageListByCompany(Context<MainState> ctx, int companyId) async {
  YllNetwork.requestPost(VillageAction.getVillageList,
      params: {'companyId': companyId}, success: (json) async {
    var villageListResult = VillageListResult.fromJson(json);
    ctx.state.villageList = villageListResult.villageList;
    ctx.state.selectedVillage = null;
    if (villageListResult.villageList != null &&
        villageListResult.villageList.isNotEmpty) {
      //获取上次选中的小区
      var sp = await SharedPreferences.getInstance();
      var lastVillageId = sp.getInt('lastVillage');
      if (lastVillageId != null) {
        for (VillageSimpleResult item in villageListResult.villageList) {
          if (item.villageId == lastVillageId) {
            ctx.state.selectedVillage = item;
            ctx.dispatch(CompanyActionCreator.initUI());
            break;
          }
        }
       }
      //如果没有找到该小区
      if (ctx.state.selectedVillage == null) {
        ctx.state.selectedVillage = villageListResult.villageList.first;
        var sp = await SharedPreferences.getInstance();
        sp.setInt('lastVillage', ctx.state.selectedVillage.villageId);
        ctx.dispatch(CompanyActionCreator.initUI());
      }
    }
    ctx.dispatch(MainActionCreator.refreshUI());
  }, failed: (errorCode, errorMsg) {
    Fluttertoast.showToast(msg: '$errorCode,$errorMsg');
  });
}
